package com.pat.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.pat.common.Constants;
import com.pat.web.ApiResponse;
import com.pat.web.model.Project;

public class ProjectController extends Controller {

	public void addUserProject() {
		Project project = getBean(Project.class, "");
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		project.setUserId(userId).setCreateTime(new Date()).setUpdateTime(new Date()).setSystem(1).save();
		ApiResponse response = new ApiResponse();
		response.setData(project.getId());
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void delUserProject() {
		int result = Db.update("update project set is_deleted=1 where user_id=? and id=?",
				getAttr(Constants.LOGIN_USER_ID), getParaToLong("id"));
		ApiResponse response = new ApiResponse();
		response.setData(result);
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void getAllProject() {
		long userId = getAttr(Constants.LOGIN_USER_ID);
		List<Project> dailyProject = Project.dao.find(
				"(select id,project_type,project_name,unit,remark,project_icon,sort_num,system from project"
						+ " where project_type = 0 and (system = 0 or user_id = ?) and is_deleted = 0 order by sort_num,create_time)",
				userId);
		List<Project> medicalProject = Project.dao.find(
				"(select id,project_type,project_name,unit,remark,project_icon,sort_num,system from project"
						+ " where project_type = 2 and (system = 0 or user_id = ?) and is_deleted = 0  order by sort_num,create_time)",
				userId);
		Map<String, List<Project>> result = new HashMap<String, List<Project>>();
		result.put("daily", dailyProject);
		result.put("medical", medicalProject);
		ApiResponse response = new ApiResponse();
		response.setData(result);
		response.setStatusCode("0000");
		renderJson(JSON.toJSONString(response));
	}
}
